package cn.telling.tianlianproduct.otherservice.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import cn.telling.tianlianproduct.common.util.TianlianConstance;
import cn.telling.tianlianproduct.otherservice.dao.AreaMapper;
import cn.telling.tianlianproduct.otherservice.service.IArea;
import cn.telling.tianlianproduct.otherservice.vo.AreaInfoBaseVo;
import cn.telling.tianlianproduct.otherservice.vo.AreaInfoVo;
import cn.telling.tianlianproduct.productrelease.service.ICommon;

/**
 * 
 * @ClassName: AreaImpl 
 * @Description: 供应商销售区域业务接口实现
 * @author 陈赟 
 * @date 2015年8月26日 下午4:57:09 
 *
 */
@Service("areaOtherService")
public class AreaImpl implements IArea {
    
    @Autowired
    @Qualifier("areaMapper")
    private AreaMapper areaMapper;
    
    @Autowired
    @Qualifier("commonService")
    private ICommon commonService;

    @Override
    public Map<String, List<AreaInfoBaseVo>> getAreasBySelectedAreaIds(String[] areaInfos, String supplyId) {
        if(areaInfos == null || areaInfos.length == 0) {
            return null;
        }
        
        List<AreaInfoVo> supplyAllLevelsSaleAreaList = commonService.getAllLevelsSaleAreaInfoForReleaseBySupplyId(supplyId);
        if(supplyAllLevelsSaleAreaList == null || supplyAllLevelsSaleAreaList.isEmpty()) {
            return null;
        }
        
        Map<String, List<AreaInfoBaseVo>> selectedAreasMap = new HashMap<String, List<AreaInfoBaseVo>>();
        for(String areaInfo : areaInfos) {
            String[] info = areaInfo.split(":");
            String areaId = info[0];
            String areaName = info[1];
            String areaLevel = info[2];
            List<AreaInfoBaseVo> tempList = new ArrayList<AreaInfoBaseVo>();
            for(AreaInfoVo areaInfoVo : supplyAllLevelsSaleAreaList) {
                // 如果选择的区域级别是全国、省级、市级的，则找其下级区域列表
                if(areaLevel.equals(TianlianConstance.AREA_LEAF_NATION) || areaLevel.equals(TianlianConstance.AREA_LEAF_PROVINCE) || areaLevel.equals(TianlianConstance.AREA_LEAF_CITY)) {
                    if(areaId.equals(areaInfoVo.getParentId())) {
                        tempList.add(areaInfoVo);
                    }
                } else {    // 如果选择的区域级别是县级的，则直接列出已选择的区域
                    if(areaId.equals(areaInfoVo.getAreaId())) {
                        tempList.add(areaInfoVo);
                        break;
                    }
                }
            }
            selectedAreasMap.put(areaId + ":" + areaName, tempList);
        }
        return selectedAreasMap;
    }

    @Override
    public Map<String, List<Map<String, Object>>> getAllLevelsAreasBySelectedAreaIds(String[] areaInfos, String supplyId) {
        Map<String, List<Map<String, Object>>> map = new HashMap<String, List<Map<String, Object>>>();
        
        if(areaInfos == null || areaInfos.length == 0) {
            return map;
        }
        
        List<AreaInfoVo> supplyAllLevelsSaleAreaList = commonService.getAllLevelsSaleAreaInfoForReleaseBySupplyId(supplyId);
        if(supplyAllLevelsSaleAreaList == null || supplyAllLevelsSaleAreaList.isEmpty()) {
            return map;
        }
        
        String selectedAreaLevel = areaInfos[0].split(":")[2];
        int areaLevel = Integer.parseInt(selectedAreaLevel);
        int leafLevel = TianlianConstance.AREA_LEAF_LEVEL;
        
        if(selectedAreaLevel.equals(TianlianConstance.AREA_LEAF_COUNTY)) {
            List<Map<String, Object>> areasList = this.getAreasList(areaInfos, supplyAllLevelsSaleAreaList, selectedAreaLevel);
            map.put("countyArea", areasList);
        } else {
            // 如果选择的区域有下一级，则递归级联出下一级区域列表
            for(int i=areaLevel+1; i<=leafLevel; i++) {
                String arealevel = String.valueOf(i);
                List<Map<String, Object>> areasList = this.getAreasList(areaInfos, supplyAllLevelsSaleAreaList, arealevel);
                if(arealevel.equals(TianlianConstance.AREA_LEAF_CITY)) {
                    map.put("cityArea", areasList);
                }
                if(arealevel.equals(TianlianConstance.AREA_LEAF_COUNTY)) {
                    map.put("countyArea", areasList);
                }
                
                if(i < leafLevel) {
                    areaInfos = this.getChildrenAreas(areaInfos, supplyAllLevelsSaleAreaList);
                }
            }
        }
        return map;
    }
    
    /**
     * 
     * @Title: getChildrenAreas 
     * @Description: 获取下级区域列表
     * @param @param areaInfos
     * @param @param supplyAllLevelsSaleAreaList
     * @param @return    设定文件 
     * @return String[]    返回类型 
     * @throws 
     * @author 陈赟
     */
    private String[] getChildrenAreas(String[] areaInfos, List<AreaInfoVo> supplyAllLevelsSaleAreaList) {
        List<AreaInfoVo> childrenList = new ArrayList<AreaInfoVo>();
        for(String areaInfo : areaInfos) {
            String[] info = areaInfo.split(":");
            String areaId = info[0];
            
            for(AreaInfoVo areaInfoVo : supplyAllLevelsSaleAreaList) {
                if(areaId.equals(areaInfoVo.getParentId())) {
                    childrenList.add(areaInfoVo);
                }
            }
        }
        
        areaInfos = new String[childrenList.size()];
        for(int j=0; j<childrenList.size(); j++) {
            AreaInfoVo areaInfoVo = childrenList.get(j);
            areaInfos[j] = areaInfoVo.getAreaId() + ":" + areaInfoVo.getAreaName() + ":" + areaInfoVo.getAreaLevel();
        }
        
        return areaInfos;
    }
    
    /**
     * 
     * @Title: getAreasList 
     * @Description: 获取当前区域的下级区域列表
     * @param @param areaInfos
     * @param @param supplyAllLevelsSaleAreaList
     * @param @param arealevel 当前区域的级别
     * @param @return    设定文件 
     * @return List<Map<String,Object>>    返回类型 
     * @throws 
     * @author 陈赟
     */
    private List<Map<String, Object>> getAreasList(String[] areaInfos, List<AreaInfoVo> supplyAllLevelsSaleAreaList, String arealevel) {
        List<Map<String, Object>> areasList = new ArrayList<Map<String, Object>>();
        for(String areaInfo : areaInfos) {
            String[] info = areaInfo.split(":");
            String areaId = info[0];
            String areaName = info[1];
            String areaLevel = info[2];
            
            List<AreaInfoBaseVo> tempList = new ArrayList<AreaInfoBaseVo>();
            // 如果当前区域是县级，因其没有下一级区域，所以返回它本身
            if(areaLevel.equals(TianlianConstance.AREA_LEAF_COUNTY)) {
                AreaInfoBaseVo areaInfoVo = new AreaInfoBaseVo();
                areaInfoVo.setAreaId(areaId);
                areaInfoVo.setAreaName(areaName);
                areaInfoVo.setAreaLevel(areaLevel);
                tempList.add(areaInfoVo);
            } else { // 如果当前区域不是县级（省、市），则找出其子级区域
                for(AreaInfoVo areaInfoVo : supplyAllLevelsSaleAreaList) {
                    if(areaId.equals(areaInfoVo.getParentId())) {
                        AreaInfoBaseVo areaInfoBaseVo = new AreaInfoBaseVo();
                        areaInfoBaseVo.setAreaId(areaInfoVo.getAreaId());
                        areaInfoBaseVo.setAreaName(areaInfoVo.getAreaName());
                        areaInfoBaseVo.setAreaLevel(areaInfoVo.getAreaLevel());
                        areaInfoBaseVo.setParentId(areaInfoVo.getParentId());
                        tempList.add(areaInfoBaseVo);
                    }
                }
            }
            
            Map<String, Object> tempMap = new HashMap<String, Object>();
            if(arealevel.equals(TianlianConstance.AREA_LEAF_CITY)) {
                tempMap.put("provinceId", areaId);
            }
            if(arealevel.equals(TianlianConstance.AREA_LEAF_COUNTY)) {
                tempMap.put("cityId", areaId);
            }
            tempMap.put("children", tempList);
            
            areasList.add(tempMap);
        }
        return areasList;
    }

    @Override
    public List<AreaInfoBaseVo> parseSelectedAreas(String[] areaInfos) {
        List<AreaInfoBaseVo> areaInfoList = new ArrayList<AreaInfoBaseVo>();
        
        if(areaInfos == null || areaInfos.length == 0) {
            return areaInfoList;
        }
        
        for(String areaInfo : areaInfos) {
            String[] info = areaInfo.split(":");
            String areaId = info[0];
            String areaName = info[1];
            String areaLevel = info[2];
            
            AreaInfoBaseVo areaInfoVo = new AreaInfoBaseVo();
            areaInfoVo.setAreaId(areaId);
            areaInfoVo.setAreaName(areaName);
            areaInfoVo.setAreaLevel(areaLevel);
            areaInfoList.add(areaInfoVo);
        }
        return areaInfoList;
    }

	@Override
	public List<AreaInfoVo> findAreaInfoByAreaIds(String[] areaIds) {
		// TODO Auto-generated method stub
		return areaMapper.findAreaInfoByAreaIds(areaIds);
	}

}
